package com.bruce.tool.web.core.util;

import com.bruce.tool.common.exception.ExceptionUtils;
import com.bruce.tool.web.core.vo.ErrorCode;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.Optional;

/**
 * 异常工具类
 *
 * @author : Bruce(刘正航) 上午10:21 2018/2/26
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class WebExceptionUtils extends ExceptionUtils {

    public static ModelAndView transferErrorInfo(Throwable ex) {
        String result = printErrorInfo(ex);
        ModelAndView mav = new ModelAndView();
        mav.addObject("code", Optional.ofNullable(getCode(ex)).orElse(ErrorCode.SYSTEM_ERROR));
        mav.addObject("message", result);
        mav.setView(new MappingJackson2JsonView());
        return mav;
    }

}
